package be.android.forap.gui;

import be.android.forap.ForAp;
import be.android.forap.ForApAuthenticator;
import be.android.forap.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class BaseActivity extends Activity{
	
	protected void onResume(){
		if(!ForAp.getInstance().isLoggedIn())
			finish();
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		View view = View.inflate(this, R.layout.exit_button, null);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setIcon(R.drawable.ic_launcher_lock);
		builder.setTitle("for Android Privacy");
		builder.setView(view);
		builder.setInverseBackgroundForced(true);
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				BaseActivity.this.finish();
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.dismiss();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();   
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.layout.menu_main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_addC:
			intent = new Intent(BaseActivity.this, ContactAdder.class);
			startActivity(intent);
			return true;
		case R.id.menu_remC:
			
			return true;
		case R.id.menu_editC:
			
			return true;
		case R.id.menu_settings:
			intent = new Intent(BaseActivity.this, SettingsActivity.class);
			startActivity(intent);
			return true;
		case R.id.menu_debug:
			intent = new Intent(BaseActivity.this, DebugActivity.class);
			startActivity(intent);
			return true;
		case R.id.menu_logout:
			ForAp.getInstance().logout();
			intent = new Intent(BaseActivity.this, ForApAuthenticator.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
